package mygame.chameleonpoker;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class PokerCards {
	// the class that define a card
	public static class Card {
		public static int Spade = 0;
		public static int Heart = 1;
		public static int Club = 2;
		public static int Diamond = 3;
		public static int Joker =4;
		
		public int pattern;
		public int number;
		
		public Card() {
			pattern = -1;
			number = -1;
		}
		
		public void Copy(Card card) {
			this.pattern = card.pattern;
			this.number = card.number;
		}
		
		public void set_to_null() {
			pattern = -1;
			number = -1;
		}
		
		public boolean is_null() {
			if(pattern == -1 && number == -1) 
				return true;
			else
				return false;
		}
	}
	
	
	private List<Card> cardsPool = new ArrayList<Card>();
	private Random rand = new Random();
	private int current_number;
	
	public PokerCards() {
		for(int i = 0; i<54; i++) {
			Card card = new Card();
			check_pattern(card,i);
			cardsPool.add(card);
		}
		shuffle();
	}
	
	public void shuffle() {
		int times = 53;
		Card card_temp = new Card();
		while(times != 0) {
			int index = rand.nextInt(times+1);
	
			// swap them
			card_temp = cardsPool.get(index);
			cardsPool.set(index, cardsPool.get(times));
			cardsPool.set(times, card_temp);
			times--;		
		}
		current_number = 54;
	}
	
	public void deal(Player player) {
		if(current_number > 0)
			player.get_card(cardsPool.get(--current_number));
	}
	
	public void check_pattern(Card card, int number) {
		if(number == 52 || number == 53) {
			card.pattern = Card.Joker;
			card.number = number - 52;
		}
		else {
			card.pattern = number/13;
			card.number = number % 13;
		}		
	}
	
	public int get_number() {
		return current_number;
	}
}
